package com.alibaba.springboot.health.service.impl;

import com.alibaba.springboot.health.dao.OrderDao;
import com.alibaba.springboot.health.pojo.Order;
import com.alibaba.springboot.health.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    private static final Logger LOGGER= LogManager.getLogger(OrderServiceImpl.class);
    @Autowired
    private OrderDao orderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer orderId) {
        return orderDao.queryById(orderId);
    }
    
    
    /**
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param order 实体
     * @return PageInfo<Order> 分页数据集合
     */
    @Override
    public PageInfo<Order> queryAll(int page, int pageSize, Order order) {
        /*拦截分页信息*/
        PageHelper.startPage(page, pageSize);

        List<Order> orderList = orderDao.queryAll(order);

        return new PageInfo<>(orderList);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        String orderNumber = System.currentTimeMillis()+"";
        Date date = new Date();
        LOGGER.info(orderNumber+"+===================+"+date);
        order.setOrderNumber(orderNumber);
        order.setOrderTime(date);
        orderDao.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        orderDao.update(order);
        return queryById(order.getOrderId());
    }

    /**
     * 通过主键删除数据
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer orderId) {
        return orderDao.deleteById(orderId) > 0;
    }
}